package com.mocchira.music.radio;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.json.JSONArray;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;

public class LatestTracksPlayListActivity extends ListActivity implements OnItemClickListener {
    
    private LatestTracksManager manager;
    private String filter;
    
    private ProgressDialog progress = null;
    
    public enum CATEGORY {
        A_I,
        J_R,
        S_Z,
        OTHERS
    }
    
    private boolean isListed(String filter, String tag) {
        char top = Character.toLowerCase(tag.charAt(0));
        if (filter.equals(CATEGORY.A_I.name())) {
            return (top >= 'a' && top <= 'i');
        } else if (filter.equals(CATEGORY.J_R.name())) {
            return (top >= 'j' && top <= 'r');
        } else if (filter.equals(CATEGORY.S_Z.name())) {
            return (top >= 's' && top <= 'z');
        } else if (filter.equals(CATEGORY.OTHERS.name())) {
            return !Character.isLetter(top);
        }
        return false;
    }
    
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            JSONArray latestList = manager.getLatestTags();
            if (latestList == null) {
                this.sendEmptyMessageDelayed(0, 1000);
                return;
            }
            List<String> result = new ArrayList<String>();
            for (int i = 0; i < latestList.length(); i++) {
                try {
                    String tag = latestList.getString(i);
                    if (isListed(filter, tag))
                        result.add(tag);
                } catch (Exception ex) {};
            }
            if (result.size() == 0) return;
            Collections.sort(result);
            String[] array = new String[1];
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(LatestTracksPlayListActivity.this, android.R.layout.simple_list_item_1, result.toArray(array));
            LatestTracksPlayListActivity.this.setListAdapter(adapter);
            if (progress.isShowing())
                try {progress.dismiss();}catch (Exception nex){};
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getIntent().getExtras();
        filter = (String)bundle.get(R.string.bundle_latest_tag + "");
        manager = LatestTracksManager.getInstance(this);
        manager.getLatestTags();
        handler.sendEmptyMessageDelayed(0, 1);
        this.getListView().setOnItemClickListener(this);
        if (progress == null)
            progress = ProgressDialog.show(LatestTracksPlayListActivity.this, "preparing...", "Download Tag List File");
    }
    
    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
        Intent intent = new Intent(this, FMRPlayActivity.class);
        Bundle bundle = new Bundle();
        String tag = (String)arg0.getItemAtPosition(position);
        bundle.putString(R.string.bundle_latest_tag+"", tag);
        intent.putExtras(bundle);
        startActivity(intent);
    }

}
